﻿using System;
using System.Collections.Generic;

using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Editor;
using ITAP.Core.Modelling.Primitives;
using System.Drawing;

namespace ITAP.ShprossesBuilder.Render
{
    public class ThumbnailRender : Render
    {
        public override List<Type> RenderPriority
        {
            get
            {
                return base.RenderPriority;
            }
            set
            {
                base.RenderPriority = value;
            }
        }
        public ThumbnailRender()
        {
            renderPriority = new List<Type>();
            renderPriority.Add(typeof(ModelContainer));

            renderPriority.Add(typeof(ModelCell));
            renderPriority.Add(typeof(Model));
            renderPriority.Add(typeof(ShprossConnector));
            renderPriority.Add(typeof(Connector));
            renderPriority.Add(typeof(ShprossConnectorStub));
            renderPriority.Add(typeof(SpacerConnector));
            renderPriority.Add(typeof(Shpross));
            renderPriority.Add(typeof(SpacerElement));
            renderPriority.Add(typeof(Spacer));            

        }

        public override void Visit(ShprossConnector shprossConnector)
        {
            base.Visit(shprossConnector);
            foreach (IComponent innerComponent in shprossConnector.InnerComponents)
                innerComponent.AcceptVisitor(this);
        }

        public override void Visit(Shpross shpross)
        {
            if (GlobalPreferences.Preferences.ShowShprossSnaps)
            {
                GlobalPreferences.Preferences.ShowShprossSnaps = false;
                base.Visit(shpross);
                GlobalPreferences.Preferences.ShowShprossSnaps = true;
            }
            else
                base.Visit(shpross);
            
        }

        protected override void DrawShpross(Shpross shpross, bool drawStartBorder, bool drawEndBorder)
        {
            if (shpross.Height != 0)
            {
                CirclePrimitive shprossCirclePrimitive = shpross.Primitives[0] as CirclePrimitive;
                this.DrawCirclePrimitive(shprossCirclePrimitive, shpross.Color, shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);


                if (shpross.Selected)
                {
                    if (shpross.Parent.Parent.Parent is ModelContainer)
                    {
                        ModelContainer container = shpross.Parent.Parent.Parent as ModelContainer;
                        if (container.SelectedComponentsStack.IndexOf(shpross) == 0)
                        {
                            this.DrawCirclePrimitive(shprossCirclePrimitive, Color.FromArgb(70, 0, 255, 90), shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);
                        }
                        else
                            this.DrawCirclePrimitive(shprossCirclePrimitive, this.SelectedComponentColor, shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);
                    }
                    else
                        throw new Exception();

                    if (GlobalPreferences.Preferences.ShowShprossSnaps)
                    {
                        this.DrawShprossHook(shpross.StartHook, Color.FromArgb(60, Color.Yellow));
                        this.DrawShprossHook(shpross.CenterHook, Color.FromArgb(60, Color.Red));
                        this.DrawShprossHook(shpross.EndHook, Color.FromArgb(60, Color.Blue));
                    }

                    switch (shpross.SelectedPoint)
                    {
                        case ShprossPoints.Start:
                            this.DrawShprossHook(shpross.StartHook, Color.Yellow);
                            break;
                        case ShprossPoints.Center:
                            this.DrawShprossHook(shpross.CenterHook, Color.Red);
                            break;
                        case ShprossPoints.End:
                            this.DrawShprossHook(shpross.EndHook, Color.Blue);
                            break;
                    }
                }
                else
                {
                    if (GlobalPreferences.Preferences.ShowShprossSnaps)
                    {
                        this.DrawShprossHook(shpross.StartHook, Color.FromArgb(60, Color.Yellow));
                        this.DrawShprossHook(shpross.CenterHook, Color.FromArgb(60, Color.Red));
                        this.DrawShprossHook(shpross.EndHook, Color.FromArgb(60, Color.Blue));
                    }
                }
            }
            else
            {
                LinePrimitive shprossLinePrimitive = shpross.Primitives[0] as LinePrimitive;
                this.DrawLinePrimitive(shprossLinePrimitive, shpross.Color, shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);

                if (shpross.Selected)
                {

                    if (shpross.Parent.Parent.Parent is ModelContainer)
                    {
                        ModelContainer container = shpross.Parent.Parent.Parent as ModelContainer;
                        if (container.SelectedComponentsStack.IndexOf(shpross) == 0)
                            this.DrawLinePrimitive(shprossLinePrimitive, Color.FromArgb(70, 0, 255, 90), shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);
                        else
                            this.DrawLinePrimitive(shprossLinePrimitive, this.SelectedComponentColor, shpross.BorderColor, 0, 1, drawStartBorder, drawEndBorder);
                    }
                    else
                        throw new Exception();

                    if (GlobalPreferences.Preferences.ShowShprossSnaps)
                    {
                        this.DrawShprossHook(shpross.StartHook, Color.FromArgb(60, Color.Yellow));
                        this.DrawShprossHook(shpross.CenterHook, Color.FromArgb(60, Color.Red));
                        this.DrawShprossHook(shpross.EndHook, Color.FromArgb(60, Color.Blue));
                    }
                    switch (shpross.SelectedPoint)
                    {
                        case ShprossPoints.Start:
                            this.DrawShprossHook(shpross.StartHook, Color.Yellow);
                            break;
                        case ShprossPoints.Center:
                            this.DrawShprossHook(shpross.CenterHook, Color.Red);
                            break;
                        case ShprossPoints.End:
                            this.DrawShprossHook(shpross.EndHook, Color.Blue);
                            break;
                    }
                }
                else
                {
                    if (GlobalPreferences.Preferences.ShowShprossSnaps)
                    {
                        this.DrawShprossHook(shpross.StartHook, Color.FromArgb(60, Color.Yellow));
                        this.DrawShprossHook(shpross.CenterHook, Color.FromArgb(60, Color.Red));
                        this.DrawShprossHook(shpross.EndHook, Color.FromArgb(60, Color.Blue));
                    }
                }
            }

        }
    }
}
